home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 428_02 / ed / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  7.7 KB  |  401 lines

  1. /*
  2. ** commands.c
  3. **
  4. ** Ed, Version 1.51, Copyright (c) 1992-94 SoftCircuits
  5. ** Redistributed by permission.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <io.h>
  13. #include <pictor.h>
  14. #include "ed.h"
  15.  
  16. static int save_result = FALSE;
  17.  
  18. #define MAX_BUFF    65
  19. static char findbuff[MAX_BUFF + 1];
  20. static char replacebuff[MAX_BUFF + 1];
  21.  
  22.  
  23. /*
  24. ** displays Using-Help help topic
  25. */
  26. void using_help()
  27. {
  28.     helprun(hlp_usinghlp);
  29.  
  30. } /* using_help */
  31.  
  32. /*
  33. ** activates help index
  34. */
  35. void help_index()
  36. {
  37.     helprun(NULL);
  38.  
  39. } /* help_index */
  40.  
  41. /*
  42. ** display program version and copyright info
  43. */
  44. void about()
  45. {
  46.     helpsetcontext(hlp_about);
  47.     messagebox(copyright,"About",MB_OK,&msgcolors);
  48.  
  49. } /* about */
  50.  
  51. /*
  52. ** set tab stop width
  53. */
  54. void set_tabwidth()
  55. {
  56.     int done = FALSE,new_width;
  57.     char buffer[15];
  58.  
  59.     helpsetcontext(hlp_tabwidth);
  60.     pushstatus();
  61.  
  62.     itoa(tab_size,buffer,10);
  63.     statusbar("Enter tab between 2 and 20");
  64.  
  65.     while(!done) {
  66.         if(editbox("Enter new tab width",buffer,3,NULL,&msgcolors)) {
  67.             new_width = atoi(buffer);
  68.             if(new_width >= 2 && new_width <= 20) {
  69.                 tab_size = new_width;
  70.                 update_state = UPDATE_REPAINT;
  71.                 update_cursor(FALSE);
  72.                 done = TRUE;
  73.             }
  74.             else {
  75.                 messagebox("You must enter a number between 2 and 20",NULL,
  76.                     MB_OK,&msgcolors);
  77.             }
  78.         }
  79.         else done = TRUE;
  80.     }
  81.     popstatus();
  82.  
  83. } /* set_tabwidth */
  84.  
  85. /*
  86. ** attempts to save the current file if:
  87. **     it has been modified
  88. **     the user wants to save it
  89. **
  90. ** returns FALSE if attempt to save fails or if the current file
  91. ** is modified and the user selects cancel from the message box
  92. */
  93. int save_modified()
  94. {
  95.     int result = TRUE;
  96.  
  97.     if(modified) {
  98.         result = messagebox("Current file has changed\n"
  99.             "Do you want to save it?",    NULL,MB_YESNOCANCEL,&msgcolors);
  100.         if(result == 2) {
  101.             filesave();
  102.             result = save_result;
  103.         }
  104.     }
  105.     return(result);
  106.  
  107. } /* prompt_save */
  108.  
  109. /*
  110. ** create a new file
  111. */
  112. void filenew()
  113. {
  114.     helpsetcontext(hlp_new);
  115.  
  116.     if(save_modified())
  117.         new_file(NULL);
  118.  
  119. } /* filenew */
  120.  
  121. /*
  122. ** load a new file from disk
  123. */
  124. void fileopen()
  125. {
  126.     char *ptr;
  127.  
  128.     helpsetcontext(hlp_open);
  129.  
  130.     if(save_modified()) {
  131.         ptr = pickfile("*.*","Open File",&msgcolors);
  132.         if(ptr != NULL)
  133.             load_file(ptr);
  134.     }
  135.  
  136. } /* fileopen */
  137.  
  138. /*
  139. ** save the current file
  140. */
  141. void filesave()
  142. {
  143.     helpsetcontext(hlp_save);
  144.  
  145.     if(!stricmp(filename,untitled))
  146.         filesaveas();
  147.     else
  148.         save_result = save_file(filename);
  149.  
  150. } /* filesave */
  151.  
  152. /*
  153. ** save the curretn file with a different name
  154. */
  155. void filesaveas()
  156. {
  157.     char buffer[65 + 1];
  158.  
  159.     helpsetcontext(hlp_saveas);
  160.  
  161.     save_result = FALSE;
  162.     *buffer = '\0';
  163.  
  164.     while(editbox("Save current file as",buffer,65,NULL,&msgcolors)
  165.         && *buffer) {
  166.         strupr(buffer);
  167.         if(!access(buffer,0x00)) {
  168.             if(!messagebox("File already exists\nDo you want to overwrite it?",
  169.                 buffer,MB_YESNO,&msgcolors)) {
  170.                 *buffer = '\0';
  171.                 continue;
  172.             }
  173.         }
  174.         save_result = save_file(buffer);
  175.         break;
  176.     }
  177.  
  178. } /* filesaveas */
  179.  
  180. /*
  181. ** called from file print to send current file to printer
  182. */
  183. static int do_print()
  184. {
  185.     int i,j,col,tab_cols;
  186.     LINE *line;
  187.  
  188.     for(line = head;line != NULL;line = line->next) {
  189.         for(col = 0,i = 0;i < line->len;i++) {
  190.             if(line->text[i] == '\t') {
  191.                 tab_cols = (tab_size - (col % tab_size));
  192.                 for(j = 0;j < tab_cols;j++) {
  193.                     if(prnputc(' '))
  194.                         return(FALSE);
  195.                 }
  196.                 col += tab_cols;
  197.             }
  198.             else {
  199.                 if(prnputc(line->text[i]))
  200.                     return(FALSE);
  201.                 col++;
  202.             }
  203.         }
  204.         if(prnputs("\r\n"))
  205.             return(FALSE);
  206.     }
  207.     /* send form feed */
  208.     if(prnputc('\x0C'))
  209.         return(FALSE);
  210.  
  211.     return(TRUE);
  212.  
  213. } /* do_print */
  214.  
  215. /*
  216. ** sends the current file to the printer
  217. */
  218. void fileprint()
  219. {
  220.     helpsetcontext(hlp_print);
  221.     if(!messagebox("Send file to printer?","Print",MB_YESNO,&msgcolors))
  222.         return;
  223.  
  224.     pushstatus();
  225.     xprintf(statusbar,"Printing %s",filename);
  226.  
  227.     if(!do_print()) {
  228.         messagebox("Error printing file","Error",MB_OK,&msgcolors);
  229.         clearerr(stdprn);
  230.     }
  231.     popstatus();
  232.  
  233. } /* fileprint */
  234.  
  235. /*
  236. ** searches the current file starting at the current location
  237. ** for the text in findbuff
  238. ** if the text is found, the file positon is move to the find location
  239. ** if the text is not found and msg == TRUE, a message is displayed
  240. */
  241. static int find_text(int msg,int start_col)
  242. {
  243.     LINE *line;
  244.     int i,row;
  245.  
  246.     pushstatus();
  247.     statusbar("Searching...");
  248.  
  249.     row = file_row;
  250.     i = start_col;
  251.     for(line = curr_line;line != NULL;line = line->next,row++) {
  252.         for( ;i < line->len;i++) {
  253.             if(toupper(line->text[i]) == toupper(*findbuff)) {
  254.                 if(!strnicmp(line->text + i,findbuff,strlen(findbuff))) {
  255.                     curr_line = line;
  256.                     file_row = row;
  257.                     line_ndx = i;
  258.                     if(row < top_row || row >= top_row + EDIT_ROWS) {
  259.                         if(row > 5) {
  260.                             top_row = row - 5;
  261.                             for(i = 0;i < 5;i++)
  262.                                 line = line->prev;
  263.                             top_line = line;
  264.                         }
  265.                         else {
  266.                             top_row = 0;
  267.                             top_line = head;
  268.                         }
  269.                     }
  270.                     popstatus();
  271.                     update_state = UPDATE_REPAINT;
  272.                     update_cursor(TRUE);
  273.                     return(TRUE);
  274.                 }
  275.             }
  276.         }
  277.         i = 0;
  278.     }
  279.     popstatus();
  280.     if(msg) {
  281.         sprintf(buffer,"Text not found:\n\"%s\"",findbuff);
  282.         messagebox(buffer,NULL,MB_OK,&msgcolors);
  283.     }
  284.  
  285.     return(FALSE);
  286.  
  287. } /* find_text */
  288.  
  289. /*
  290. ** searches for the specified text
  291. */
  292. void search()
  293. {
  294.     helpsetcontext(hlp_find);
  295.  
  296.     if(editbox("Find",findbuff,MAX_BUFF,NULL,&msgcolors) && *findbuff)
  297.         find_text(TRUE,line_ndx + 1);
  298.  
  299. } /* search */
  300.  
  301. /*
  302. ** locates and changes the specified text
  303. */
  304. void change()
  305. {
  306.     static MEDITSTRUCT medit[] = {
  307.         { "Find",            findbuff,    MAX_BUFF },
  308.         { "Replace with", replacebuff,MAX_BUFF },
  309.     };
  310.     char buffer[31];
  311.     int key,len1,len2,count = 0,changeall = FALSE;
  312.     int skip = FALSE,first_time = TRUE;
  313.  
  314.     helpsetcontext(hlp_change);
  315.  
  316.     if(multiedit(medit,2,"Change",&msgcolors)) {
  317.         len1 = strlen(findbuff);
  318.         len2 = strlen(replacebuff);
  319.         if(len1 == 0)
  320.             return;
  321.         if(len2 == 0) {
  322.             if(!messagebox("Blank replacement string\nText will be deleted",
  323.                 "Warning",MB_OKCANCEL,&msgcolors))
  324.                 return;
  325.         }
  326.         while(find_text(first_time,line_ndx + skip)) {
  327.             first_time = FALSE;
  328.             skip = FALSE;
  329.             if(!changeall) {
  330.                 statusbar("Replace this occurance [Y/N]?  <A>=Replace all "
  331.                     " <Escape>=Done  <F1>=Help");
  332.  
  333.                 while(TRUE) {
  334.                     key = kbdread();
  335.                     if(key == ESCAPE_KEY)
  336.                         break;
  337.                     else if(key == F1_KEY) {
  338.                         helprun(hlp_change);
  339.                         continue;
  340.                     }
  341.                     key = toupper(key & 0xFF);
  342.                     if(key == 'A') {
  343.                         changeall = TRUE;
  344.                         break;
  345.                     }
  346.                     else if(key == 'Y')
  347.                         break;
  348.                     else if(key == 'N') {
  349.                         skip = TRUE;
  350.                         break;
  351.                     }
  352.                     else beep();
  353.                 }
  354.                 if(key == ESCAPE_KEY)    /* escape */
  355.                     break;
  356.                 if(skip)                 /* skip this one */
  357.                     continue;
  358.             }
  359.             else if(kbdready()) {
  360.                 if(kbdread() == ESCAPE_KEY)
  361.                     break;
  362.                 else
  363.                     beep();
  364.             }
  365.             if(len1 > len2)
  366.                 shrink_line(&curr_line,line_ndx,len1 - len2);
  367.             else if(len1 < len2)
  368.                 if(!expand_line(&curr_line,line_ndx,len2 - len1))
  369.                     break;
  370.             memmove(curr_line->text + line_ndx,replacebuff,len2);
  371.             show_line(curr_line,(file_row - top_row) + edit_top);
  372.  
  373.             /* don't search replaced text */
  374.             line_ndx += len2;
  375.             update_cursor(TRUE);
  376.  
  377.             modified = TRUE;
  378.             count++;
  379.         }
  380.         show_status();
  381.         if(!first_time) {
  382.             sprintf(buffer,"%d change(s) made",count);
  383.             messagebox(buffer,NULL,MB_OK,&msgcolors);
  384.         }
  385.     }
  386.  
  387. } /* change */
  388.  
  389. /*
  390. ** repeats the last search
  391. */
  392. void repeatsearch()
  393. {
  394.     if(*findbuff != '\0') {
  395.         helpsetcontext(hlp_repeatfind);
  396.         find_text(TRUE,line_ndx + 1);
  397.     }
  398.     else search();
  399.  
  400. } /* repeatsearch */
  401.